package com.blue.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

public class FreeMarkerHelper {

	private FreeMarkerConfigurer freemarker;

	/**
	 * 根据模板生成HTML
	 * 
	 * @param data
	 *            数据模型
	 * @param templateFolder
	 *            模板存在目录，如：/template或/WEB-INF/views/template
	 * @param templateName
	 *            模板文件名称
	 * @param out
	 *            HTML输出路径，如：c:/test.html
	 */
	public void generator(String tplName, String out, Map<String, Object> data) {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {

			Template template = getTemplate(tplName);

			fos = new FileOutputStream(new File(out));
			osw = new OutputStreamWriter(fos, "UTF-8");
			bw = new BufferedWriter(osw, 1024);

			template.process(data, bw);

		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != bw) {
					bw.flush();
					bw.close();
				}
				if (null != osw) {
					osw.flush();
					osw.close();
				}
				if (null != fos) {
					fos.flush();
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 返回模板生成后的字符串
	 * 
	 * @param data
	 *            数据模型
	 * @param tplName
	 *            模板名称
	 * @return 生成后的HTML字符串
	 */
	public String generator(String tplName, Map<String, Object> data) {
		try {
			return FreeMarkerTemplateUtils.processTemplateIntoString(getTemplate(tplName), data);
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据模板生成HTML，用于junit调试
	 * 
	 * @param data
	 *            数据模型
	 * @param templateFolder
	 *            模板存在目录，如：src/main/webapp/template
	 * @param templateName
	 *            模板文件名称
	 * @param out
	 *            HTML输出路径，如：c:/test.html
	 */
	public void generatorDebug(String templateFolder, String templateName, String out, Map<String, Object> data) {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {
			this.freemarker.getConfiguration().setDirectoryForTemplateLoading(new File(templateFolder));
			Template template = getTemplate(templateName);

			fos = new FileOutputStream(new File(out));
			osw = new OutputStreamWriter(fos, "UTF-8");
			bw = new BufferedWriter(osw, 1024);

			template.process(data, bw);

		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != bw) {
					bw.flush();
					bw.close();
				}
				if (null != osw) {
					osw.flush();
					osw.close();
				}
				if (null != fos) {
					fos.flush();
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 用于junit调试，不设置ServletContext
	 * 
	 * @param data
	 *            数据模型
	 * @param templateFolder
	 *            模板存放目录路径，如：src/main/webapp/template
	 * @param templateName
	 *            模板名称
	 */
	public void writeConsoleDebug(String templateFolder, String templateName, Map<String, Object> data) {
		try {
			this.freemarker.getConfiguration().setDirectoryForTemplateLoading(new File(templateFolder));
			Template template = getTemplate(templateName);
			template.process(data, new PrintWriter(System.out));
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出到控制台，用于WEB调试
	 * 
	 * @param data
	 *            数据模型
	 * @param templateName
	 *            模板名称
	 */
	public void writeConsoleDebug(String templateName, Map<String, Object> data) {
		try {
			Template template = getTemplate(templateName);
			template.process(data, new PrintWriter(System.out));
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	public Template getTemplate(String templateName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
		return this.freemarker.getConfiguration().getTemplate(templateName);
	}

	public FreeMarkerConfigurer getFreemarker() {
		return freemarker;
	}

	public void setFreemarker(FreeMarkerConfigurer freemarker) {
		this.freemarker = freemarker;
	}

}
